<?php

namespace plugin\seckill\event;

use app\model\order\OrderDetail;
use plugin\seckill\app\api\service\OrderService;
use plugin\seckill\app\model\SeckillOrder;

class RefundSeckillStock
{
    /**
     *  返还秒杀库存
     * @param $param
     * @return bool
     */
    public function handle($param)
    {
        $applyRefundData = json_decode($param['apply_refund_data'], true)['order_num_data'];

        $detailId2Num = [];
        foreach ($applyRefundData as $vo) {
            $detailId2Num[$vo['order_detail_id']] = $vo['num'];
        }

        $orderDetail = (new OrderDetail())->findOne([
            'order_id' => $param['order_id']
        ], ['rule','id'])['data'];

        $seckillInfo = (new SeckillOrder())->findOne([
            'order_id' => $param['order_id']
        ], 'seckill_id')['data'];

        $orderInfo = [
            'seckill_id' => $seckillInfo['seckill_id'],
            'sku' => $orderDetail['rule'],
            'num' => $detailId2Num[$orderDetail['id']]
        ];

        return (new OrderService())->refundStockAndSales($orderInfo);
    }
}
